package model.roomManagement.Implementation;


import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;

import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import view.LoginView;

import controller.roomManagement.Implementation.LoginController;
import controller.roomManagement.Implementation.RoomManageController;
import controller.roomManagement.Interface.ILoginController;

import model.infocenter.ServerInformations;
import model.roomManagement.Interface.ILoginModel;
import model.roomManagement.Interface.IRoomManageModel;
import model.roomManagement.Interface.IServer;
import model.roomManagement.Interface.IUser;




public class LoginModel implements ILoginModel {
    private IServer server;
    private String ip;
    private IUser user;
    static String serverIp=null;
    int serverPort=1099;
    
	public LoginModel(){
	
	}
	

	public static String getServerIp() {
		return serverIp;
	}


	public static void setServerIp(String serverIp) {
		LoginModel.serverIp = serverIp;
		ServerInformations.getInstance().setIP(serverIp);
	}


	public int login(int id, String password) throws ConnectException {
		int login=0;
		
		try {
			star();
		ip=(InetAddress.getLocalHost()).getHostAddress() ;

 		login=server.loginCheck(id,password);
		
		
		if(login==1){//成功登录

			user=server.getUserById(id);

		}
		
		} catch (Exception e) {
			 throw new ConnectException();
		}
		return login;
	}

	public IServer getServer() {
		return server;
	}


	public IUser getUser() {
		return user;
	}


	public void quit() {
		System.exit(0);
		
	}

	


	public void star() throws ConnectException {
	       try{
	    	   String URL="//"+serverIp+":"+serverPort+"/Server";
	    	   System.out.println(URL);
			server = (IServer)Naming.lookup(URL);
	       }
	       catch(Exception e){
	    	   e.printStackTrace();
	    	   throw new ConnectException();
	       }
	}
	
	public static void main(String[] argv) { 
		
		LoginModel LM=new LoginModel();
		ILoginController LC=new LoginController(LM); 
	    LoginView lv = new LoginView(LC,LM);
	    lv.open();

	}
	
}
